﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DBUtility;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class DGmapoverlay
    {
        

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tgmapoverlay");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.VarChar,20)
                };
            parameters[0].Value = id;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.GmapoverlayInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tgmapoverlay(");
            strSql.Append("OverlayName,Overlaytype,City,Longitude,Latitude,Star,Remark)");
            strSql.Append(" values (");
            strSql.Append("@OverlayName,@Overlaytype,@City,@Longitude,@Latitude,@Star,@Remark)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@OverlayName", MySqlDbType.VarChar,100),
					new MySqlParameter("@Overlaytype", MySqlDbType.VarChar,100),
					new MySqlParameter("@City", MySqlDbType.VarChar,45),
					new MySqlParameter("@Longitude", MySqlDbType.Double,20),
					new MySqlParameter("@Latitude", MySqlDbType.Double,20),
                    new MySqlParameter("@Star", MySqlDbType.Int32,10),
					new MySqlParameter("@Remark", MySqlDbType.Text)};
           
            parameters[0].Value = model.Name;
            parameters[1].Value = model.Type;
            parameters[2].Value = model.City;
            parameters[3].Value = model.Longitude;
            parameters[4].Value = model.Latitude;
            parameters[5].Value = model.Star;
            parameters[6].Value = model.Remark;
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.GmapoverlayInfo model)
        {   // OverlayName,Overlaytype,City,Longitude,Latitude,Star,Remark
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tgmapoverlay set ");
            strSql.Append("OverlayName=@OverlayName,");
            strSql.Append("Overlaytype=@Overlaytype,");
            strSql.Append("City=@City,");
            strSql.Append("Longitude=@Longitude,");
            strSql.Append("Latitude=@Latitude,");
            strSql.Append("Star=@Star,");
            strSql.Append("Remark=@Remark");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@OverlayName", MySqlDbType.VarChar,100),
					new MySqlParameter("@Overlaytype", MySqlDbType.VarChar,100),
					new MySqlParameter("@City", MySqlDbType.VarChar,45),
					new MySqlParameter("@Longitude", MySqlDbType.Double,20),
					new MySqlParameter("@Latitude", MySqlDbType.Double,20),
                    new MySqlParameter("@Star", MySqlDbType.Int32,10),
					new MySqlParameter("@Remark", MySqlDbType.Text),
                    new MySqlParameter("@id", MySqlDbType.Int32,10)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.Type;
            parameters[2].Value = model.City;
            parameters[3].Value = model.Longitude;
            parameters[4].Value = model.Latitude;
            parameters[5].Value = model.Star;
            parameters[6].Value = model.Remark;
            parameters[7].Value = model.id;
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tgmapoverlay ");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
};
            parameters[0].Value = id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tgmapoverlay ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.GmapoverlayInfo GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,OverlayName,Overlaytype,City,Longitude,Latitude,Star,Remark ");
            strSql.Append("from tgmapoverlay  where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
};
            parameters[0].Value = id;

            Model.GmapoverlayInfo model = new Model.GmapoverlayInfo();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OverlayName"] != null && ds.Tables[0].Rows[0]["OverlayName"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["OverlayName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Overlaytype"] != null && ds.Tables[0].Rows[0]["Overlaytype"].ToString() != "")
                {
                    model.Type = ds.Tables[0].Rows[0]["Overlaytype"].ToString();
                }
                if (ds.Tables[0].Rows[0]["City"] != null && ds.Tables[0].Rows[0]["City"].ToString() != "")
                {
                    model.City = ds.Tables[0].Rows[0]["City"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Longitude"] != null && ds.Tables[0].Rows[0]["Longitude"].ToString() != "")
                {
                    model.Longitude = double.Parse(ds.Tables[0].Rows[0]["Longitude"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Latitude"] != null && ds.Tables[0].Rows[0]["Latitude"].ToString() != "")
                {
                    model.Latitude = double.Parse(ds.Tables[0].Rows[0]["Latitude"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Star"] != null && ds.Tables[0].Rows[0]["Star"].ToString() != "")
                {
                    model.Star = int.Parse(ds.Tables[0].Rows[0]["Star"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Remark"] != null && ds.Tables[0].Rows[0]["Remark"].ToString() != "")
                {
                    model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  id,OverlayName,Overlaytype,City,Longitude,Latitude,Star,Remark ");
            strSql.Append("FROM tgmapoverlay ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        public DataSet GetList(string parameter, string parameterVaule)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  id,OverlayName,Overlaytype,City,Longitude,Latitude,Star,Remark");
            strSql.Append(" FROM tgmapoverlay ");
            strSql.Append(" where ");
            strSql.Append(parameter);
            strSql.Append(" like @A");
            MySqlParameter[] parameters = {
                                              new MySqlParameter("@A", MySqlDbType.Text)
                                          };
            parameters[0].Value = "%" + parameterVaule + "%";
            return DbHelperMySQL.Query(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tgmapoverlay ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
    }
}
